import 'package:flutter/material.dart';

RichText getStyledText(String text, String wordToStyle, TextStyle selectedStyle,
    TextStyle nonSelectedStyle) {
  List<TextSpan> spans = [];
  int start = 0;
  int index;

  // Find all occurrences of the wordToStyle and add them to spans with different styles.
  while ((index = text.indexOf(wordToStyle, start)) != -1) {
    if (index > start) {
      // Add the part of the string before the word to style
      spans.add(TextSpan(text: text.substring(start, index)));
    }
    // Add the word to style with a different style
    spans.add(
      TextSpan(text: wordToStyle, style: selectedStyle),
    );
    start = index + wordToStyle.length;
  }

  // Add the remaining part of the string after the last occurrence
  if (start < text.length) {
    spans.add(TextSpan(text: text.substring(start)));
  }

  return RichText(
    textAlign: TextAlign.center,
    text: TextSpan(
      style: nonSelectedStyle, // Default style for unstyled text
      children: spans,
    ),
  );
}
